package com.example.score.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.score.entity.Course;
import com.example.score.service.CourseService;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;
import java.util.List;
/**
 * @author shenrui
 * @date 2021/9/27
 * @description 管理员对课程的增删改查等操作
 */
@Controller
public class CourseController {
    @Autowired
    CourseService courseService;

    /**
     *@description 查询课程列表
     * @return String
     */
    @GetMapping("/course_list")
    public String toCourseList(HttpSession session) {
        List<Course> courseList = courseService.list();
        session.setAttribute("courseList", courseList);
        return "course_list";
    }

    /**
     *@description 跳转到修改课程页面
     * @return String
     */
    @GetMapping("/course_revise_jump/{id}")
    public String reviceCourseJump(@PathVariable("id") Long id,
                                    HttpSession session) {
        Course course = courseService.getById(id);
        session.setAttribute("courseInfo",course);
        return "redirect:/course_revise";
    }

    /**
     *@description 修改课程信息
     * @return String
     */
    @PostMapping("/submit_course_info")
    public String reviceCourseInfo(Course course, HttpSession session, Model model) {
        //填入信息不能为空
        if (StringUtils.isNullOrEmpty(course.getCourseName()) || StringUtils.isNullOrEmpty(course.getCourseCode())) {
            model.addAttribute("stuMsg", "信息不能为空");
            return "course_revise";

        }
        //进行更新
        Boolean bool = courseService.updateById(course);
        if (bool) {
            return "redirect:/course_list";
        } else {
            model.addAttribute("stuMsg", "修改失败");
            return "course_revise";
        }

    }

    /**
     *@description 添加课程
     * @return String
     */
    @PostMapping("/submit_course_add")
    public String courseAdd(Course course, HttpSession session, Model model) {
        //填入信息不能为空
        if (StringUtils.isNullOrEmpty(course.getCourseName()) || StringUtils.isNullOrEmpty(course.getCourseCode())) {
            model.addAttribute("courseMsg", "信息不能为空");
            return "admin_course_add";

        }
        //代码不能重复
        if (courseService.count(new LambdaQueryWrapper<Course>().eq(Course::getCourseCode, course.getCourseCode())) > 0) {
            model.addAttribute("courseMsg", "代码重复");
            return "admin_course_add";
        }
        //判断是否成功插入数据库
        if(courseService.save(course)) {
            return "redirect:/course_list";
        } else {
            model.addAttribute("courseMsg", "添加失败");
            return "admin_course_add";
        }
    }
}

